<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>扫描机器</title>
	</head>
	<link rel="stylesheet" href="css/AllStyle.css" />
	<link rel="stylesheet" href="css/barcodeScan.css" />
    <script type="text/javascript" src="js/common.js" ></script>
    <script type="text/javascript" src="js/data.js" ></script>
    <script type="text/javascript" src="js/tmpl.js" ></script>
	<script type="text/javascript" src="js/echarts/common.js" ></script>
	<body>
	    <section>
	    	<p id="ProductID"></p>
	    </section>

		<section>
			<form>
	    	<div id="bcid">
				<p class="tip"><img width="200px" src="images/loading.gif"><i>摄像头读取中...</i></p>
			</div>
			<div class="Scanfooter">
				<ul>
					<li class='mui-action-back'>
						<p><img src="images/return.png"></p>
						<p>返回</p>
					</li>
					<li>
						<p onclick="tallyBook()"><img src="images/View.png"></p>
						<p>查看</p>
					</li>
					<li onclick="scanPicture();">
						<p><img src="images/Photo.png"></p>
						<p id="xz">相册</p>
					</li>
					<li id="loading5">
						<p><img src="images/Submit.png"></p>
						<p><button type="button" style="border:none;background: none;color:#00AAFA;font-size:1em;" onclick="updateDataToServer();">提交</button></p>
					</li>
				</ul>
			</div>

			<div style="display: none;">
	            <div class="form-group">
	                <label class="col-xs-3 control-label">产品</label>
	                <div class="col-xs-9">
	                    <span id="pid"></span>
	                </div>
	            </div>
	            <div class="form-group">
	                <label class="col-xs-3 control-label">发货网点</label>
	                <div class="col-xs-9">
	                    <span id="from_branch"></span>
	                </div>
	            </div>
	            <div class="form-group">
	                <label class="col-xs-3 control-label">收货网点</label>
	                <div class="col-xs-9">
	                    <span id="to_branch"></span>
	                </div>
	            </div>
			</div>

			<input name="scanlist" id="scanlist"
           onkeypress="if (event.keyCode == 13) {
                           proccessTextArea();
                           return false;
                       }"
           placeholder="多个条形码请以英文','逗号隔开" >
           </form>
	    </section>
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript">
	$etk = $_GET['etk'];
	$stype = $_GET['type'];
	$sindex = $_GET['index'];
	mui.init();
	var ws=null,wo=null;
	var scan=null,domready=false;
	// H5 plus事件处理
	function plusReady(){
		if(ws||!window.plus||!domready){
			return;
		}
		// 获取窗口对象
		ws=plus.webview.currentWebview();
		wo=ws.opener();
		// 开始扫描
		ws.addEventListener('show',function(){
			scan=new plus.barcode.Barcode('bcid');
		    scan.onmarked=onmarked;
		    scan.start({conserve:true,filename:"_doc/barcode/"});
		});
		// 显示页面并关闭等待框
	    ws.show("pop-in");
	    //wo.evalJS("closeWaiting()");
	}

	if(window.plus){
		plusReady();
	}else{
		document.addEventListener("plusready",plusReady,false);
	}

	// 监听DOMContentLoaded事件
	document.addEventListener("DOMContentLoaded",function(){
		domready=true;
		plusReady();
	},false);

	// 二维码扫描成功
	function onmarked(type,result,file){
	    switch(type){
	    	case plus.barcode.QR:
	    	type = "QR";
	    	break;
	    	case plus.barcode.EAN13:
	    	type = "EAN13";
	    	break;
	    	case plus.barcode.EAN8:
	    	type = "EAN8";
	    	break;
	    	default:
	    	type = "其它";
	    	break;
	    }
			result = getItemNo(result);
	    if($etk == 1){
			mui.init({
				beforeback: function(){
					//获得扫描界面的webview
					var supper = plus.webview.getWebviewById('supper.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(supper,'supper',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
			});
			// window.location.href='frame.html?id='+result;
			mui.back();
	    }else if($etk == 2){
	    	mui.init({
	    		beforeback: function(){
	    			//获得扫描界面的webview
	    			var taskList = plus.webview.getWebviewById('taskList.html');
	    			//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
	    			mui.fire(taskList,'taskList',{id:result});
	    			//返回true，继续页面关闭逻辑
	    			return true;
	    		}
	    	});
	    	mui.back();
	    }else if($etk == 3){
			mui.init({
				beforeback: function(){
					//获得扫描界面的webview
					var qualityList = plus.webview.getWebviewById('qualityList.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(qualityList,'qualityList',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
			});
			mui.back();

	    }else if($etk == 4){
	    	mui.init({
		    	beforeback: function(){
					//获得扫描界面的webview
					var frameList = plus.webview.getWebviewById('frameList.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(frameList,'frameList',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
		    });
	    	mui.back();
	    }else if($etk == 5){
	    	mui.init({
		    	beforeback: function(){
					//获得扫描界面的webview
					var taskList = plus.webview.getWebviewById('taskList.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(taskList,'taskList',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
		    });
	    	mui.back();
	    }else if($etk == 6){
			mui.init({
				beforeback: function(){
					//获得扫描界面的webview
					var procList = plus.webview.getWebviewById('procList.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(procList,'procList',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
			});
			mui.back();
		}else if($etk == 7){
			mui.init({
				beforeback: function(){
					//获得扫描界面的webview
					var scanReporting = plus.webview.getWebviewById('scanReporting.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(scanReporting,'scanReporting',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
			});
			mui.back();
		}else if($etk == 8){
	    	mui.init({
		    	beforeback: function(){
					//获得扫描界面的webview
					var myQualityList = plus.webview.getWebviewById('myQualityList.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(myQualityList,'myQualityList',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
		    });
	    	mui.back();
		}else if($etk == 9){
			mui.init({
				beforeback: function(){
					//获得扫描界面的webview
					var myReportingList = plus.webview.getWebviewById('myReportingList.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(myReportingList,'myReportingList',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
			});
			mui.back();
	    }else if($etk == 101){
	    	mui.init({
		    	beforeback: function(){
					//获得扫描界面的webview
					var sindex = $sindex+'.html'
					var scan = plus.webview.getWebviewById(sindex);
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(scan,'scan',{id:result,type:$stype});
					//返回true，继续页面关闭逻辑
					return true;
				}
		    });
	    	mui.back();
	    }else if($etk == 102){
			mui.init({
				beforeback: function(){
					//获得扫描界面的webview
					var frameProcList = plus.webview.getWebviewById('frameProcList.html');
					//触发扫描界面的自定义事件（refresh）,从而进行数据刷新
					mui.fire(frameProcList,'frameProcList',{id:result});
					//返回true，继续页面关闭逻辑
					return true;
				}
			});
			mui.back();
		}
	}

	// 从相册中选择二维码图片
	function scanPicture() {
	    plus.gallery.pick(function(path){
		    plus.barcode.scan(path,onmarked,function(error){
				plus.nativeUI.alert( "无法识别此图片" );
			});
	    },function(err){
	        plus.nativeUI.alert("Failed: "+err.message);
	    });
	}

	function tallyBook(){
		if($("#ProductID").text() == ''){
			alert('没有商品编号...')
		}else{
			alert($("#ProductID").text());
		}
	}

	/* 数据  */
	var sh;
	var datalist = new Array();
	var pid = "";
	var company = "";
	$(document).ready(function () {
	    $("#totalCount").html(0);
	    $("#listdata").html("");
	    pid = getCookie("scan.pid");
	    lot_no = getCookie("scan.lot_no");
	    from_bid = getCookie("scan.from_bid");
	    is_del = getCookie("scan.is_del");
	    to_bid = getCookie("scan.to_bid");
	    to_name = getCookie("scan.to_name");
	    $("#pid").text(getCookie("scan.pname"));
	    $("#from_branch").text(getCookie("scan.from_name"));
	    $("#to_branch").text(getCookie("scan.to_name"));
	    //$("#pid").text(getCookie("scan.pid"));

	    //window.onbeforeunload = function (event) {
	    //return confirm("确定退出吗");
	    //    $(window).unbind('beforeunload');
	    //}


	});

//	function TRsetTimeout(){
//		setTimeout(function () {
//	        $('.TheResults').hide().html('<img width="100px" src="images/loading5.gif">');
//
//	    },3000);
//	}

	function updateDataToServer() {
		var prid = $("#ProductID").text();
		$("#scanlist").val(prid);
	    var item_no = $("#scanlist").val();
	    if(empty(item_no)){
	        alertMessage('条形码不能为空');
	        return false;
	    }
	    $('#loading5').html('<img style="width:50px" src="images/loading6.gif">');
	    var datas = {access_token:getToken(),item_no:item_no,pid:pid,from_branch:from_bid,to_branch:to_bid,lot_no:lot_no,is_del:is_del};
	    $.ajax({
	        type: "post",
	        url: getServerUrl() + "/Open/Comtag/delivery",
	        dataType: 'json',
	        data: datas,
	        success: function (json) {
	            if (json.result == "1") {
	            	alert("发送数据成功")
	            	$('#loading5').html('<p><img src="images/Submit.png"></p><p><button type="button" style="border:none;background: none;color:#00AAFA;font-size:1em;" onclick="updateDataToServer();">提交</button></p>');
//	                $(".TheResults").show(300).html("发送[" + to_branch+ "]的出货数据到平台成功！<br/>");
//	                TRsetTimeout();
	            } else {
	            	alert("上传异常[" + json.error_description + "]")
	            	$('#loading5').html('<p><img src="images/Submit.png"></p><p><button type="button" style="border:none;background: none;color:#00AAFA;font-size:1em;" onclick="updateDataToServer();">提交</button></p>');
//	                $(".TheResults").show(300).html("上传出货数据到平台异常:[" + json.error_description + "]");
//	                TRsetTimeout();
	            }
	        }
	    });
	}

	function onsuccess() {
	    alert('上传完成');
	}

	mui('body').on('tap', '.myFirstPage', function() {
		mui.openWindow({
			url:'supper.html',
			id:'supper.html',
		})
	});
	</script>
</html>
